
package com.canyinghao.cananimation.demo;

import com.canyinghao.cananimation.CanAnimation;
import com.canyinghao.cananimation.CanObjectAnimator;

import ohos.aafwk.ability.Ability;

import ohos.aafwk.content.Intent;

import ohos.agp.animation.Animator;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import static com.canyinghao.cananimation.CanAnimation.animationSequence;
import static com.canyinghao.cananimation.CanAnimation.animationTogether;

/**
 * @ProjectName: CanAnimation
 * @ClassName: AnimatorAbility
 * @Description: 属性动画
 * @Author: lr
 * @CreateDate: 2021/2/24 10:34
 */
public class AnimatorAbility extends Ability {
    static final HiLogLabel logLabel = new HiLogLabel(HiLog.LOG_APP, 0x00101, "canAnimation");
    StackLayout animContainer;
    Button start_animator;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_animator);
        start_animator = (Button) findComponentById(ResourceTable.Id_start_animator);
        animContainer = (StackLayout) findComponentById(ResourceTable.Id_animContainer);

        start_animator.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                start_animator.setVisibility(Component.HIDE);
                playAnimations();
            }
        });

        animContainer.setBindStateChangedListener(new Component.BindStateChangedListener() {
            @Override
            public void onComponentBoundToWindow(Component component) {
                playAnimations();
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {

            }
        });
    }

    private void playAnimations() {
        animContainer.removeAllComponents();
        Point center = new Point(animContainer.getWidth() / 2, animContainer.getHeight() / 2);
        int size = 70;

        for (int i = 0; i < 6; i++) {
            DependentLayout.LayoutConfig params = new DependentLayout.LayoutConfig(size, size);
            final Text view = new Text(this);
            view.setLayoutConfig(params);
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.BLUE.getValue()));
            view.setBackground(shapeElement);
            view.setText("" + i);
            view.setTextColor(Color.WHITE);
            view.setTextAlignment(TextAlignment.CENTER);
            view.setTextSize(30);

            Point newPos = new Point();
            switch (i) {
                case 0:
                    newPos.modify(0, 0);
                    break;

                case 1:
                    newPos.modify(0, center.getPointY());
                    break;

                case 2:
                    newPos.modify(0, center.getPointY() * 2 - size);
                    break;

                case 3:
                    newPos.modify(center.getPointX() * 2 - size, 0);
                    break;

                case 4:
                    newPos.modify(center.getPointX() * 2 - size, center.getPointY());
                    break;

                case 5:
                    newPos.modify(center.getPointX() * 2 - size, center.getPointY() * 2 - size);
                    break;
                default:
            }

            animContainer.addComponent(view);
            CanObjectAnimator builder = CanObjectAnimator.on(view);
            int finalI = i;
            animationSequence(
                    animationTogether(
                            builder.moveTo(center.getPointX(), newPos.getPointX(), center.getPointY(), newPos.getPointY(), 2),
                            builder.rotateTo(360, 2)
                    ),
                    animationTogether(
                            builder.scaleTo(0.5f, 0.5f, 1),
                            builder.rotateTo(720, 1)
                    ),
                    animationTogether(
                            builder.moveTo(newPos.getPointX(), center.getPointX(), newPos.getPointY(), center.getPointY(), 2),
                            builder.scaleTo(0.5f, 2, 0.5f, 2, 2),
                            builder.rotateTo(360, 2)
                    ),
                    CanAnimation.run(new Runnable() {
                        @Override
                        public void run() {
                            if (finalI == 5) {
                                start_animator.setVisibility(Component.VISIBLE);
                            }
                        }
                    })
            ).start();
        }
    }


    @Override
    protected void onBackground() {
        super.onBackground();
        animationSequence().cancel();
    }
}
